import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ScrollPickerView extends StatefulWidget {
  final List<String> items;
  final int initialIndex;
  final Function(int index, String value)? onSelectedItemChanged;
  final double itemHeight;
  final int visibleItemCount;

  const ScrollPickerView({
    Key? key,
    required this.items,
    this.initialIndex = 0,
    this.onSelectedItemChanged,
    this.itemHeight = 40.0,
    this.visibleItemCount = 5,
  }) : super(key: key);

  @override
  ScrollPickerWidgetState createState() => ScrollPickerWidgetState();
}

class ScrollPickerWidgetState extends State<ScrollPickerView> {
  late FixedExtentScrollController _scrollController;
  int _selectedIndex = 0;

  @override
  void initState() {
    super.initState();
    _selectedIndex = widget.initialIndex;
    _scrollController = FixedExtentScrollController(
      initialItem: widget.initialIndex,
    );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.itemHeight * widget.visibleItemCount,
      child: Stack(
        children: [
          // 选中项背景
          Positioned(
            top: widget.itemHeight * ((widget.visibleItemCount - 1) / 2),
            left: 20.w,
            right: 20.w,
            height: widget.itemHeight,
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white.withOpacity(.1),
                borderRadius: BorderRadius.circular(8),
                border: Border.all(
                  color: Theme.of(context).colorScheme.primary.withOpacity(0.3),
                  width: 1,
                ),
              ),
            ),
          ),
          // 滚动列表
          ListWheelScrollView.useDelegate(
            controller: _scrollController,
            itemExtent: widget.itemHeight,
            perspective: 0.005,
            diameterRatio: 1.2,
            physics: const FixedExtentScrollPhysics(),
            onSelectedItemChanged: (index) {
              setState(() {
                _selectedIndex = index;
              });
              if (widget.onSelectedItemChanged != null) {
                widget.onSelectedItemChanged!(index, widget.items[index]);
              }
            },
            childDelegate: ListWheelChildBuilderDelegate(
              childCount: widget.items.length,
              builder: (context, index) {
                bool isSelected = index == _selectedIndex;
                return Container(
                  height: widget.itemHeight,
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 20.w),
                  child: Text(
                    widget.items[index],
                    style: TextStyle(
                        fontSize: isSelected ? 20.sp : 16.sp,
                        fontWeight:
                            isSelected ? FontWeight.w600 : FontWeight.w400,
                        color: isSelected
                            ? Colors.white
                            : Colors.white.withOpacity(.4)),
                    textAlign: TextAlign.center,
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
